tree grammar JKlasyTreeWalker;

options {
	tokenVocab=JKlasy;
	ASTLabelType=Tree;
}

@header {
package compiler.treewalker;
}

program
	: ^(PROGRAM main_part class_definition*) 
	;

main_part
	: ^(MAINPART variable_declaration* statements)
	;
	
class_definition
	: ^(CLASSDEFINITION ID operation_descriptor* 
	  		variable_declaration* operation_algorithm*)
	;
	
variable_declaration
	: ^(VARIABLEDECLARATION 
		(declaration_type ID)+
		)
		
	;

declaration_type
	: ^(DECLARATIONTYPE simple_type (^(ARRAY NUMBER))?)
	;

parameter_type
	: ^(PARAMETERTYPE simple_type (^(ARRAY NUMBER))?)
	;
	
simple_type
	: INTT
	| BOOLT
	| STRINGT
	| REALT
	| CHART
	| ID
	;

operation_descriptor
	: ^(OPERATIONDESCRIPTOR designator (parameter_type+)? ^(RESULT parameter_type?))
	;
	
designator
	: ID
	| operator
	;

operation_algorithm
	: ^(OPERATIONALGORITHM designator ^(ARGUMENTS ((ID+)?)) 
	  	  ^(VARIABLES variable_declaration*) statements (^(RETURN expression))?)
	;

statements
	: ^(STATEMENTS statement*)
	;

statement
    : assign_statement
	| if_statement 
	| while_statement
	| input_statement
	| output_statement
	| call_statement
	;

if_statement 
	: ^(IFSTATEMENT expression conditional_option conditional_option?)
	;
	
conditional_option
	: statement
	| statements
	;
	
assign_statement
	: ^(ASSIGNSTATEMENT ID expression)  	
	;
	
call_statement
	: ^(CALLSTATEMENT call)
	;

call
	: ^(CALL simple_call ID*)
	;
	
simple_call
	: ^(SIMPLECALL ID expression*)
	;
	
while_statement 
	: ^(WHILESTATEMENT expression statements)
	;
	  
input_statement
	: ^(INPUTSTATEMENT ID+)	
	;
			
output_statement
	: ^(OUTPUTSTATEMENT ID+)
	;
	
expression
	: ^(EXPRESSION expr)
	;

expr
	: ^(EQUAL expr expr)
	| ^(NOTEQ expr expr)
	| ^(GR expr expr)
	| ^(GREQ expr expr)
	| ^(LS expr expr)
	| ^(AND expr expr)
	| ^(OR expr expr)
	| ^(PLUS expr expr)
	| ^(MINUS expr expr)
	| ^(DIV expr expr)
	| ^(MOD expr expr)
	| ^(MULT expr expr)
	| NUMBER
	| TRUE
	| FALSE
	| STRING
	| CHAR
	| NULL
	| ^(ID (^(INDEX expr))? )
	| ^(NOT expr)
	| ^(CAST expr simple_type)
	| call
	;
	
boolean_operator:
	  AND
	| OR
	;
	
relational_operator:
	  EQUAL
	| NOTEQ
  	| GR 
  	| GREQ
  	| LS 
  	| LEQ
	;

additive_operator:
	  PLUS
	| MINUS
	;

multiplicative_operator:
	  DIV 
	| MOD
	| MULT
	;	

operator
	: relational_operator
	| additive_operator
	| multiplicative_operator
	| boolean_operator
	;	

